/*
Copyright (c) 2009, Dan Hagerstrand and contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Cirrus Creative nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef BENG_RESOURCE_H
#define BENG_RESOURCE_H

#include <vector>

#include "BengGlobal.h"

class BengSystem;

/// The BengResource class is designed to hold media loaded externally. It consists of a list of several items of the same type which can be retrieved
/// at any time. The list acts as a container of data which can be completely removed when necessary, simplifying resource management.
///
/// Ideally, a resource will contain related items, such as all the sprites to one character. In fact, a BengResource<BengISprite> is used internally
/// by the layer and drawing routines to define a tileset. Since the class is templated, the container can hold any type you wish. Your class will need
/// to follow this skeleton in order to be compatible:
///
/// MyClass::MyClass(const char *filepath)\n
/// {\n
///     this->data = myLoadDataFunction(filepath);\n
///
///     if (!this->data)\n
///        return;\n
///
///     data_->myInitializationFunctions();\n
/// }\n
///
/// MyClass::~MyClass(void)\n
/// {\n
///     if (this->data);\n
///         myUnloadDataFunction(this->data);\n
/// }\n
///
/// void *MyClass::getRawData(void)\n
/// {\n
///     return this->data;\n
/// }\n

template <typename T>
class BengResource {

    private:

    bool isDirty_; // TODO: Implement

    protected:

    std::vector<T*> dataList;

    public:

    BengResource<T>(void)
    {
        this->isDirty_ = false;
    }

    ~BengResource<T>(void)
    {
        this->clearData();
    }

    T *loadData(const char *filepath)
    {
        T *newresource = new T(filepath);

        if (!newresource->getRawData())
        {
            return NULL;
        }

        this->dataList.push_back(newresource);

        return newresource;
    }

    T *getData(unsigned int index)
    {
        if (this->dataList.size() > index)
            return this->dataList[index];
        else
            return NULL;
    }

    int getSize(void)
    {
        return this->dataList.size();
    }

    void clearData(void)
    {
        while (this->dataList.size() > 0)
        {
            delete this->dataList[this->dataList.size() - 1];
            this->dataList.pop_back();
        }
    }

};

/// Where the BengResource class is intended to keep a list of same-type media, the BengResourceList class exists to manage multiple BengResource instances.
/// Thus you have a BengResourceList<BengISprite> that manages all resources of type BengISprite. This system has two main methods: First, the create() method
/// instantiates and indexes a new BengResource, adding a single entry to it in order to occupy it. Secondly, the append() method takes any existing BengResource
/// and adds another entry to the end. Once you are done with one resource, you can start a new one using the create() method once again.

template <typename T>
class BengResourceList {

    private:

    std::vector<BengResource<T>*> list_;

    BengSystem *system_; // Optional, allows the class to interface with the system.

    public:

    BengResource<T> *create(const char *filepath)
    {
        BengResource<T> *newresource = new BengResource<T>;
        //BengResource<BengISprite> *newresource;

        if (!newresource->loadData(filepath))
        {
            if (!this->system_)
            {
                delete newresource;
                return NULL;
            }
            else
            {
                this->system_->giveError(BENG_ERROR_FATAL, "Unable to load resource data!\n%s", filepath);
            }
        }

        this->list_.push_back(newresource);

        return newresource;

    }

    T *append(const char *filepath, BengResource<T> *existing)
    {
        T *newentry = existing->loadData(filepath);

        if (!newentry)
        {
            if (!this->system_)
            {
                return NULL;
            }
            else
            {
                this->system_->giveError(BENG_ERROR_FATAL, "Unable to load resource data!\n%s", filepath);
            }
        }

        return newentry;
    }

    void clear(void)
    {
        while (this->list_.size() > 0)
        {
            delete this->list_[this->list_.size() - 1];
            this->list_.pop_back();
        }
    }

    BengResourceList<T>(void) {};
    BengResourceList<T>(BengSystem *parentSystem) // TODO: Fix up along with the BengScene::attachToSystem() crap
    {
        this->system_ = parentSystem;
    }

    ~BengResourceList<T>(void)
    {
        this->clear();
    }
};

#endif
